/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 报告期类型
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="Rpt_ReportPeriod_Type")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ReportPeriodType implements Comparable<Object>, Serializable {
	
	private static final long serialVersionUID = -2089802388549569870L;

	@Id
	@Column(name="ReportPeriod_Type_GUID",length=38)
	@GenericGenerator(name="ReportPeriodType-assigned",strategy="assigned")
	private String guid;
	
	@Column(name="ReportPeriod_Type_Code",length=16)
	private String code;
	
	@Column(name="ReportPeriod_Type_Name",length=50)
	private String name;
	
	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="reportPeriodType")
	private List<ReportPeriod> reportPeriods;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="Parent_ReportPeriod_Type_GUID",nullable=true)
	private ReportPeriodType parentReportPeriodType;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="service_guid",nullable=true)
	private Service service;

	@Transient
	public String getParentGuid() {
		if(parentReportPeriodType != null){
			return parentReportPeriodType.getGuid();
		}
		return "";
	}
	
	public void setParentGuid(String parentGuid) {
		if(parentGuid != null && parentGuid.trim().length()>0){
			if(parentReportPeriodType==null){
				parentReportPeriodType = new ReportPeriodType();
				parentReportPeriodType.setGuid(parentGuid);
			}
		}
	}
	
	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public List<ReportPeriod> getReportPeriods() {
		return reportPeriods;
	}

	public void setReportPeriods(List<ReportPeriod> reportPeriods) {
		this.reportPeriods = reportPeriods;
	}

	public ReportPeriodType getParentReportPeriodType() {
		return parentReportPeriodType;
	}

	public void setParentReportPeriodType(ReportPeriodType parentReportPeriodType) {
		this.parentReportPeriodType = parentReportPeriodType;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		ReportPeriodType myClass = (ReportPeriodType) object;
		return new CompareToBuilder().append(this.code, myClass.code).append(this.guid, myClass.guid).append(this.name, myClass.name).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof ReportPeriodType)) {
			return false;
		}
		ReportPeriodType rhs = (ReportPeriodType) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(this.code, rhs.code).append(this.guid, rhs.guid).append(this.name, rhs.name).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-1733583597, -1074306745).appendSuper(super.hashCode()).append(this.code).append(this.guid).append(this.name).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("name", this.name).append("code", this.code).append("guid", this.guid).toString();
	}

}